package bby.automation.module1;

import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.testng.*;
import org.testng.annotations.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class Signup extends bby.automation.module1.pages.TestBase {
  private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();

  @Test
  public void testSignup() throws Exception {
    driver.get(baseUrl + "/");
    driver.findElement(By.id("ctl00_Footer_ctl00_foot_HyperLink24")).click();
    driver.findElement(By.id("ctl00_CP_FirstNameContainer_TxtFirstName")).clear();
    driver.findElement(By.id("ctl00_CP_FirstNameContainer_TxtFirstName")).sendKeys("Dmytro");
    driver.findElement(By.id("ctl00_CP_LastNameContainer_TxtLastName")).clear();
    driver.findElement(By.id("ctl00_CP_LastNameContainer_TxtLastName")).sendKeys("Mazur");
    driver.findElement(By.id("ctl00_CP_EmailContainer_TxtEmail")).clear();
    driver.findElement(By.id("ctl00_CP_EmailContainer_TxtEmail")).sendKeys("dmtrqa@mailinator.com");
    driver.findElement(By.id("ctl00_CP_PostalCodeContainer_TxtZipCode")).clear();
    driver.findElement(By.id("ctl00_CP_PostalCodeContainer_TxtZipCode")).sendKeys("V5A4B7");
    driver.findElement(By.id("ctl00_CP_EmailNotificationUC_repEmailNotificationOptions_ctl00_chkEmailNotificationOption")).click();
    driver.findElement(By.id("ctl00_CP_BtnSubmit")).click();
    try {
      assertTrue(isElementPresent(By.id("ctl00_CP_confirmationMessage_ConfirmationMessage")));
    } catch (Error e) {
      verificationErrors.append(e.toString());
    }
  }

  private boolean isElementPresent(By by) {
    try {
      driver.findElement(by);
      return true;
    } catch (NoSuchElementException e) {
      return false;
    }
  }

  private String closeAlertAndGetItsText() {
    try {
      Alert alert = driver.switchTo().alert();
      String alertText = alert.getText();
      if (acceptNextAlert) {
        alert.accept();
      } else {
        alert.dismiss();
      }
      return alertText;
    } finally {
      acceptNextAlert = true;
    }
  }
}
